package com.sduept.nwld.dataserver.controller.document;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.document.DocumentDirManager;
import com.sduept.nwld.dataserver.model.AppDocumentDir;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.primefaces.PrimeFaces;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Named
@ViewScoped
public class DocumentDirSelector extends AbstractController implements
		Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4306592078623341306L;
	@Autowired
	private DocumentDirManager docManager;

	private TreeNode root;
	private TreeNode currentSelectedNode;
	
	private List<AppDocumentDir> documentDirs;// 所有的文档目录
	
	@PostConstruct
	public void init() {
		initTree();
	}

	public void initTree() {
		documentDirs = docManager.findAllDocumentDir();
		root = new DefaultTreeNode();
		List<AppDocumentDir> rootDirs = new ArrayList<>();
		for (AppDocumentDir dir : documentDirs) {
			if (dir.getPid() == null) {
				rootDirs.add(dir);
			}
		}
		sortByOrderNo(rootDirs);
		for (AppDocumentDir d : rootDirs) {
			new LibTreeNode(AppDocumentDir.class.getSimpleName(), d, root);
		}
	}
	public void onNodeExpand(NodeExpandEvent event) {
		TreeNode currentNode = event.getTreeNode();
		if ("AppDocumentDir".equals(currentNode.getType())) {
			AppDocumentDir currentObj = (AppDocumentDir) currentNode.getData();
			List<AppDocumentDir> temps = new ArrayList<>();
			for (AppDocumentDir d : documentDirs) {
				if (d.getPid() != null && d.getPid().equals(currentObj.getId())) {
					temps.add(d);
				}
			}
			sortByOrderNo(temps);
			for (AppDocumentDir d : temps) {
				new LibTreeNode("AppDocumentDir", d, currentNode);
			}
		}
	}
	public void selectDir() {
		//PrimeFaces.current().dialog().closeDynamic(currentSelectedNode.getData());
		PrimeFaces.current().dialog().closeDynamic(currentSelectedNode.getData());
	}


	/*
	 * 按orderNo升序 排序
	 */
	private void sortByOrderNo(List<AppDocumentDir> dirs) {
		Collections.sort(dirs, new Comparator<AppDocumentDir>() {
			@Override
			public int compare(AppDocumentDir o1, AppDocumentDir o2) {
				return o1.getOrderNo() - o2.getOrderNo();
			}
		});
	}


	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getCurrentSelectedNode() {
		return currentSelectedNode;
	}

	public void setCurrentSelectedNode(TreeNode currentSelectedNode) {
		this.currentSelectedNode = currentSelectedNode;
	}


}
